package com.cetc.nsfx.service.sys;

import com.cetc.nsfx.base.BaseService;
import com.cetc.nsfx.mapper.SysUserMapper;
import com.cetc.nsfx.util.EncryptUtils;
import com.cetc.nsfx.util.Ognl;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/12/25 0025.
 */
@Service
public class UserService extends BaseService{

    public static final Log logger = LogFactory.getLog(UserService.class.getName());

    @Autowired
    SysUserMapper userMapper;

    public Map<String,Object> getUserByUserIdPassword(String USER_ID,String PASSWORD){
        /*if(1==1){
            Map<String, Object> user = new HashMap<String, Object>();
            user.put("SWRY_DM","admin");
            user.put("USERPASSWORD","21232f297a57a5a743894a0e4a801fc3");

            return user;
        }*/
        Map<String,String> params = new HashMap<String,String>();
        params.put("USER_ID",USER_ID);
        if(PASSWORD != null){
            params.put("PASSWORD",PASSWORD);
        }

        try {
            List<Map<String, Object>> list = userMapper.getUserList(params);
            if (Ognl.isNotEmpty(list)) {
                logger.debug("success getUser:"+USER_ID+","+PASSWORD);
                return list.get(0);
            }else{

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public Map<String,Object> getUserByUserId(String USER_ID){
        return this.getUserByUserIdPassword(USER_ID,null);
    }
    
    public Map<String, Object> loadUser(String loginName,String sSearch,int iDisplayStart,int iDisplayLength){
    	Map<String, Object> rs = new HashMap<String,Object>();
		 Map<String, Object> p = new HashMap<String,Object>();
		 
		 p.put("loginName", loginName);
		 p.put("sSearch", sSearch);
		 
		 List<Map<String,Object>> loadUserList = this.getSqlSessionTemplate().selectList("com.cetc.nsfx.mapper.SysUserMapper.loadUser", p,
	                new PageBounds((iDisplayStart/iDisplayLength)+1,iDisplayLength));
		 
		 @SuppressWarnings("unchecked")
		PageList<Map<String,Object>> pageList = (PageList<Map<String,Object>>)loadUserList;
		 
		 rs.put("rs", pageList);
		 rs.put("count", pageList.getPaginator().getTotalCount());
		 return rs;
	 }
    
    public boolean resetpwd(String loginName){
    	String newpwd = EncryptUtils.encryptMD5(loginName);
    	try{
    		userMapper.updpwd(newpwd,loginName);
    		return true;
    	}catch(Exception e){
    		e.printStackTrace();
    		return false;
    	}
    }
}
